<?php
/**
 * Router for example applications.
 */
class Router extends Psa_Plugin_Router{

	function psa_main(){
		
		// global config array from config.php file
		global $PSA_CFG;
		
		// remove query string from url
		$request_uri_arr = explode('?',$_SERVER["REQUEST_URI"]);
		
		// If application root folder is in subfolder from server root like: www.example.com/my/application/
		// remove unnecessary part of path. In case of url above that would be '/my/application'.
		// This value should be set in $PSA_CFG['folders']['basedir_web'] config variable in psa config.php file
		// or even better in site specific config_override.php file.
		if($PSA_CFG['folders']['basedir_web'])	
			$request_uri_arr[0] = str_replace($PSA_CFG['folders']['basedir_web'] . '/','',$request_uri_arr[0]);
		else
			// remove first '/' if application is in the web server root folder
			$request_uri_arr[0] = substr($request_uri_arr[0], 1);
		
		// explode REQUEST_URI and find controller name
		$request_uri_arr = explode('/',$request_uri_arr[0]);
		
		// controller name should be the first element of the array
		$controller_name = $request_uri_arr[0];
		
		// if controller name is 'app2' call Controller_App2 controller
		// else call 'Controller_App1' which is our default controller.
		if($controller_name == 'app2'){
			
			$controller_name = 'Controller_App2';
			
			// start session
			session_start();
			
			// restore plugin object from session
			Psa_Plugin::get_instance()->session_restore();
			
			// remove controller name ('app2') from $request_uri_arr 
			// because we want that first element of array is action name
			$request_uri_arr = array_slice($request_uri_arr, 1);
		}
		else{
			// in application 1 we don't want to use sessions so let's read data about plugins 
			// from database on every request
			Psa_Plugin::get_instance()->set_data();
			
			$controller_name = 'Controller_App1';
		}
		
		// We want to call psa_main() method of the controller and pass $request_uri_arr to the controller 
		// because the same array will be needed there to choose actions (models and views)
		$run_plugins[$controller_name]['psa_main'] = array($request_uri_arr);
		
		// call controller.
		psa_run_plugins($run_plugins);
	}
}
?>